/**
 * Created by duandazhi on 2016/12/19.
 * 订单列表
 */

$(document).ready(function () {
    //查看详情
    $('#lookSome').click(function () {
        var updateArray = [];
        var typeArray = [];
        $(".ckSelect").each(function () {
            var check = $(this).is(":checked");
            if (check) {
                var _id = $(this).attr("ckId");
                updateArray.push(_id);
                typeArray.push($(this).attr("ckType"));
            }
        });
        if (updateArray.length == 0) {
            myAlert("请选择要查看的项");
            return;
        }
        if (updateArray.length > 1) {
            myAlert("每次只能查看一项");
            return;
        }
        $.ajax({
            url: root + 'cms/order/findOrderByIdDetail',
            type: 'post',
            dataType: 'json',
            data: {
                modelId: updateArray[0],
                orderType: typeArray[0]
            },
            success: function (data) {
                console.log(data);
                if (data.code == 1) {
                    //填充数据
                    var contentHtml = $("#detailContentTmple").render(data, {mydata: data});
                    $("#detail_content").html(contentHtml);
                    $('#orderDetailModal').modal();
                } else {
                    myAlert(data.message);
                }
            }
        });
    });
    //确定核销
    $('#confirmTrain').click(function () {
        var orderType;
        var orderSatus;
        var checkError = false;
        var delArray = [];
        $(".ckSelect").each(function () {
            var check = $(this).is(':checked');
            if (check) {
                orderType = parseInt($(this).attr("ckType"));
                orderSatus = parseInt($(this).attr("ckorderstatus"));
                var _id = $(this).attr("ckId");
                delArray.push(_id);
            }
        });
        if (delArray.length == 0) {
            myAlert("请选择要操作的的项");
            return;
        }
        if (delArray.length != 1) {
            myAlert("只能操作单个订单");
            return;
        }
        if (orderSatus != 1) {
            myAlert("只能操作已经支付的订单");
            return;
        }
        myConfirm("你确定该服务已经使用？", function () {
            $.ajax({
                url: operateStatsUrl,
                type: 'post',
                data: {
                    orderId: delArray.join(","),
                    orderState: 3  //已经使用,已经消费,已经收货
                },
                dataType: 'json',
                success: function (data) {
                    if (data.code == 1) {
                        dipscontent(getfilter(), contentUrl);
                        $("#_selectAll").attr("checked", false);
                    } else {
                        myAlert(data.msg);
                    }
                }
            });
        });
    });
    //导出
    $(document).on('click', '#dataexport', function () {
        //获取数据长度
        var tr = $("#mycontent").find('tr').length;
        if (tr == 0) {
            myAlert("没有数据,不能导出！");
            return;
        } else {
            var delArray = [];
            $(".ckSelect").each(function () {
                var check = $(this).is(':checked');
                if (check) {
                    var _id = $(this).attr("ckId");
                    delArray.push(_id);
                }
            });
            if (delArray.length == 0) {
                myAlert("请选择你要导出的数据!");
            }
            //导出选中项
            else if (delArray.length > 0) {
                var modelIds = delArray.join(",");
                location.href = root + 'cms/order/fileDownload?modelIds=' + modelIds;
            }
        }
    });
});